package com.lishe.lishe.lishe.module.excitation.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.utils.BitmapUtils;
import com.lishe.lishe.lishe.utils.PermissionUtils;
import com.lishe.lishe.lishe.utils.UMShareUtils;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.wxapi.WXEntryActivity;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

/**
 * Created by yang on 2017/10/31.
 * 叫HR加入礼舍
 */

public class CallHRJoinActivity extends BaseActivity implements View.OnClickListener {


    public static void jump(Activity activity){
        Intent intent=new Intent(activity,CallHRJoinActivity.class);
        activity.startActivity(intent);
    }

    private static final String TAG=CallHRJoinActivity.class.getSimpleName();

    private Button mBack;
    private TextView mTitle,mShare;



    @Nullable
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_hr_join);
        initView();
        initListener();
        registerBroad();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(CallHRJoinActivity.this).onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "requestCode = " + requestCode + " , resultCode = " + resultCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private void registerBroad() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(WXEntryActivity.action_success);
        filter.addAction(WXEntryActivity.action_cancle);
        filter.addAction(WXEntryActivity.action_faild);
        registerReceiver(receiver, filter);
    }

    private void initView() {
        mBack=findViewById(R.id.title_left_button);
        mTitle=findViewById(R.id.title_text_title);
        mShare=findViewById(R.id.share);

        mTitle.setText("喊HR加入礼舍");
    }

    private void initListener() {
        mBack.setOnClickListener(this);
        mShare.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left_button:
                finish();
                break;
            case R.id.share:
                if (PermissionUtils.isStorageWritePermission(CallHRJoinActivity.this)) {
                    mShare.setVisibility(View.GONE);
                    UMShareUtils.getInstance().sharedImageBitmapMessage(CallHRJoinActivity.this, "", BitmapUtils.getScreenShot(CallHRJoinActivity.this, Utils.dip2px(CallHRJoinActivity.this,51)), umShareListener);
                    mShare.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

    private String share_media;
    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA share_media) {
            Log.e(TAG, "onStart = " + share_media.toString());
            CallHRJoinActivity.this.share_media = share_media.toString();
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
            Log.e(TAG, "onResult share_media =" + share_media);
            String channel=null;
            if (share_media.toString().equals("QZONE")) {
                channel="QQShare";
                toast("成功分享到QQ空间");
            } else if (share_media.toString().equals("QQ")) {
                channel="QQChat";
                toast("成功分享到QQ");
            }
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
            Log.e(TAG, "onError  throwable.getMessage=" + throwable.getMessage());
            String message = throwable.getMessage();
            toast(subMessage(message));
        }

        /**
         * 获取错误信息
         * @param message
         */
        private String subMessage(String message) {
            int start = message.indexOf("错误信息：");
            int end = message.indexOf("点击查看错误");
            return message.substring(start == -1 ? 0 : start, end == -1 ? message.length() : end);
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
            Log.e(TAG, "onCancel share_media = " + share_media);
            toast("取消分享");
        }
    };

    /**
     * 接收微信回调WXEntryActivity发送的广播
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case WXEntryActivity.action_success:
                    String channel;
                    if (share_media.equals("WEIXIN")) {
                        channel="WeChat";
                        toast("成功分享到微信");
                    } else {
                        channel="WeShare";
                        toast("成功分享到微信朋友圈");
                    }
                    break;
                case WXEntryActivity.action_cancle:
                    toast("取消分享");
                    break;
                case WXEntryActivity.action_faild:
                    toast("分享失败");
                    break;
            }
        }
    };
}
